import Vue from 'vue'
import Router from 'vue-router'
import Apple from '../components/Apple'
import RedApple from '../components/RedApple'
import Banana from '../components/Banana'
import Course from '../components/course/Course'
import Login from '../components/Login'
import localStorage from '../utils/MyLocalStorage'

Vue.use(Router)

const router = new Router ({
// export default new Router({
  mode: 'history',
  routes: [
    {
      path: '/word/input',
      name: 'word-input',
      component: () => import('../components/word/WordInput.vue')
      // redirect:'/word/login'
      // children: [
      //   // {
      //   //   path: 'word/login',
      //   //   name: 'word-word',
      //   //   component: () => import('../components/word/WordLogin.vue'),
      //   // },
      //   {
      //     path: 'word/input',
      //     name: 'word-input',
      //     component: () => import('../components/word/WordInput.vue'),
      //   },
      //   {
      //     path: 'word/detail',
      //     name: 'word-check-detail',
      //     component: () => import('../components/word/WordCheckDetail.vue'),
      //   },
      // ]

    },
    // {
    //   path: '/',
    //   name: 'HelloWorld',
    //   component: HelloWorld
    // },

    // {
    //   //:param 传参方式
    //   path: '/apple/:color/find/:id',
    //   name: 'Apple',
    //   component: Apple
    // },
    // 重定向
    {
      path: '/',
      redirect:'/course/course'
    },
    {
      //:param 传参方式
      path: '/course/login',
      name: 'Login',
      component: Login

    },
    {
      //:param 传参方式
      path: '/course/course',
      name: 'Course',
      component: Course

    },
    {
      //:param 传参方式
      path: '/apple',
      name: 'Apple',
      component: Apple,
      children: [
        {
          path: 'red',
          name: 'redApple',
          component: RedApple,
        }
      ]

    },
    {
      path: '/banana',
      name: 'Banana',
      component: Banana
    }
  ]},
);

//路由拦截
router.beforeEach((to, from, next) => {
  //to到哪儿 from从哪儿离开 next跳转 为空就是放行
  let toPath = to.path;
  console.log('router-toPath' + toPath)
  if(toPath.indexOf("word/login")>0){
    next();
  }
  //可放行的地址
  const passPath= ["/word/login", "/course/login","/word/input","/word","/word/detail"];

  //如果跳转为登录，就放行
  if(passPath.indexOf(toPath)>-1){
  // if(toPath.indexOf("course/login")>0){
    // console.log("去登陆")
    next();
  // console.log("to.path "+to.path);
  // if (to.path === '/course/login') {
  //
  //   next();
  } else {
    //取出localStorage判断
    // let token = localStorage.getItem(this.$api.tokenKey);
    let token = localStorage.get('token');
    if (token == null || token === '') {
      console.log('请先登录')
      next({name: 'Login'});
    } else {
      next();
    }
  }


})


export default router;
